Syntax
ListView(
children: [
//widget items
],
)
OR
ListView(
children: [
child widget1,
child widget2,
.....
],
scrollDirection: Axis.horizontal,
)
Using custom widget
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
// the root widget of our application
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("Exploring Widgets"),
),
body: myWidget(),
),
);
}
}
Widget myWidget() {
return ListView.builder(
padding: EdgeInsets.all(16.0),
// spacing of the rows
itemExtent: 30.0,
// provides an infinite list
itemBuilder: (BuildContext context, int index) {
return Text('Row $index');
},
);
}
On tap
Widget myWidget() {
return ListView.builder(
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text('Row $index'),
onTap: () {
// do something
},
);
},
);
}
Listview with container
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(
title: Text('TutorialKart - Flutter ListView'),
),
body: ListView(
children: [
Container(
height: 50,
color: Colors.lime[800],
child: const Center(child: Text('Apple')),
),
Container(
height: 50,
color: Colors.lime[600],
child: const Center(child: Text('Banana')),
),
Container(
height: 50,
color: Colors.lime[400],
child: const Center(child: Text('Mango')),
),
Container(
height: 50,
color: Colors.lime[200],
child: const Center(child: Text('Orange')),
),
],
),
),
);
}
}
Listview with Card
ListView(
children: [
Card(
child: ListTile(
title:Text('List Item 1') ,
)
),
Card(
child: ListTile(
title:Text('List Item 2') ,
),
),
Card(
child: ListTile(
title:Text('List Item 3') ,
)
),
],
padding: EdgeInsets.all(10),
)
Listview with listTile
ListView(
children: [
ListTile(
leading: CircleAvatar(
backgroundColor: Colors.white,
backgroundImage: AssetImage('images/avatar.png'),
),
title: Text(
'John Judah',
),
subtitle: Text('2348031980943'),
trailing: Icon(Icons.keyboard_arrow_right),
onTap: () {
Text('Another data');
},
),
new Divider(
height: 1.0,
indent: 1.0,
),
ListTile(
leading: CircleAvatar(
backgroundColor: Colors.white,
backgroundImage: AssetImage('images/avatar_purple.png'),
),
title: Text('Bisola Akanbi'),
subtitle: Text('2348031980943'),
trailing: Icon(Icons.keyboard_arrow_right),
onTap: () {
Text('Another data');
},
onLongPress: () {
Text('Data');
},
),
new Divider(
height: 1.0,
indent: 1.0,
),
ListTile(
leading: CircleAvatar(
backgroundColor: Colors.white,
backgroundImage: AssetImage('images/avatar_brown.png'),
),
title: Text('Eghosa Iku'),
subtitle: Text('2348031980943'),
trailing: Icon(Icons.keyboard_arrow_right),
),
new Divider(
height: 1.0,
indent: 1.0,
),
]
)
Data looping in listview
// main.dart
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
// Remove the debug banner
debugShowCheckedModeBanner: false,
title: 'Kindacode.com',
theme: ThemeData(
primarySwatch: Colors.green,
),
home: HomePage());
}
}
class HomePage extends StatelessWidget {
// Generate some dummy data
final List